/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.wsinfra.wsr.impl;

import java.util.Calendar;
import java.util.Date;

import org.apache.xmlbeans.GDuration;




/** 

@author Sima Patel
$Date: 2005/05/04 18:05:34 $
$Revision: 1.3 $
*/


public class WsrUtil {
	 private static final WsrUtil instance = new WsrUtil();
	 private String moduleName = "WsrUtil: ";
	 
	 public static WsrUtil getInstance() {
	     return instance;
	 }
	 /** checks the validity of Group or Message Expiry Time */
	 public boolean isExpiryTimePassed(Calendar expiryTime){
	 	Calendar calendar = Calendar.getInstance();
	     if(calendar.get(Calendar.YEAR) < expiryTime.get(Calendar.YEAR)){
	      	System.out.println("Current year is less than expirytime year");
	      	return false;
	      }else if(calendar.get(Calendar.YEAR) == expiryTime.get(Calendar.YEAR)){
	      	if(calendar.get(Calendar.MONTH)< expiryTime.get(Calendar.MONTH)){
	      		return false;
	      	}else if(calendar.get(Calendar.MONTH)== expiryTime.get
	      	        (Calendar.MONTH)){
	      		if(calendar.get(Calendar.DATE)< expiryTime.get(Calendar.DATE)){
	      			return false;
	      		}else if(calendar.get(Calendar.DATE)== expiryTime.get
	      		        (Calendar.DATE)){
	      			if(calendar.get(Calendar.HOUR)<expiryTime.get(Calendar.HOUR)){
	      				return false;
	      			}else if(calendar.get(Calendar.HOUR)== expiryTime.get
	      			        (Calendar.HOUR)){
	      				if(calendar.get(Calendar.MINUTE)< expiryTime.get
	      				        (Calendar.MINUTE)){
	      					return false;
	      				}else if(calendar.get(Calendar.MINUTE)== expiryTime.get
	      				        (Calendar.MINUTE)){
	      					if(calendar.get(Calendar.SECOND) < expiryTime.get
	      					        (Calendar.SECOND)){
	      						return false;
	      					}else{
	      					  return true;	
	      					}
	      				}else{
	      					return true;
	      				}
	      			}else{
	      				return true;
	      			}
	      		}else{
	      			return true;
	      		}
	      		
	      	}else{
	      		return true;
	      	}
	      }else{
	      	return true;
	      }		

	 }
	 /** checks the validity of GroupMaxIdleDuration */
	 public boolean isDurationPassed(GDuration duration){
	     Date currentTime = new Date();
	     GDuration currentDuration = new GDuration(+1, currentTime.getYear(),
	                                             currentTime.getMonth(),
	                                             currentTime.getDate(),
	                                             currentTime.getHours(),
	                                             currentTime.getMinutes(),
	                                             currentTime.getSeconds(), null);
	     int comparison = currentDuration.compareToGDuration(duration);
	     if(comparison < 0){
	         return false;
	     }else{
	         return true;
	     }
	 }
	 /** to help database insertion of Calendar value
	   *this method convert it into the String value*/
	  public String getStringOfCalendar(Calendar calendar){
	    String calendarmonth;
	    String calendardate;
	    String calendaryear;
	    String calendarhour;
	    String calendarsecond;
	    String calendarminute;
	    String stringCalendar="";
	    if(calendar != null){
	    calendaryear = ""+calendar.get(Calendar.YEAR);
	    if(calendar.get(Calendar.MONTH) >= 1 && calendar.get(Calendar.MONTH) <10){
	      calendarmonth = "0" + calendar.get(Calendar.MONTH);
	    }else{
	      calendarmonth =""+calendar.get(Calendar.MONTH);
	    }

	    if(calendar.get(Calendar.DATE) >= 1 && calendar.get(Calendar.DATE) <10){
	      calendardate = "0" + calendar.get(Calendar.DATE);

	    }else{
	      calendardate =""+calendar.get(Calendar.DATE);
	    }
	    if(calendar.get(Calendar.HOUR) >= 1 && calendar.get(Calendar.HOUR) <10){
	      calendarhour = "0" + calendar.get(Calendar.HOUR);
	    }else if(calendar.get(Calendar.HOUR) == 12){
		calendarhour = "12";
	    }else if(calendar.get(Calendar.HOUR) == 0){
		calendarhour ="00";
	    }else{
	      calendarhour =""+calendar.get(Calendar.HOUR);
	    }

	   if(calendar.get(Calendar.MINUTE) >= 1 && calendar.get(Calendar.MINUTE) <10){
	     calendarminute = "0" + calendar.get(Calendar.MINUTE);
	   }else if(calendar.get(Calendar.MINUTE) == 60){
	       calendarminute = "60";
	   }else if(calendar.get(Calendar.MINUTE) == 0){
	       calendarminute ="00";
	   }else{
	     calendarminute =""+calendar.get(Calendar.MINUTE);
	    }

	   if(calendar.get(Calendar.SECOND) >= 1 && calendar.get(Calendar.SECOND) <10){
	     calendarsecond = "0" + calendar.get(Calendar.SECOND);
	   }else if(calendar.get(Calendar.SECOND) == 60){
	       calendarsecond ="60";
	   }else if(calendar.get(Calendar.SECOND) == 0){
	       calendarsecond ="00";
	   }else{
	      calendarsecond =""+calendar.get(Calendar.SECOND);
	    }
	   stringCalendar = calendaryear+"-"+calendarmonth+"-"+calendardate+"-"+
	           calendarhour+"-"+calendarminute+"-"+calendarsecond+" ";
	   }
	   return stringCalendar;
	      
	  }
	  /** to help database insertion of GDuration value
	   *this method convert it into the String value*/
	  public String getStringOfDuration(GDuration duration){
	    String durationmonth;
	    String durationdate;
	    String durationyear;
	    String durationhour;
	    String durationsecond;
	    String durationminute;
	    String stringDuration="";
	    if(duration != null){
	    durationyear = ""+duration.getYear();
	    if(duration.getMonth() >= 1 && duration.getMonth() <10){
	      durationmonth = "0" + duration.getMonth();
	    } else{
	      durationmonth =""+duration.getMonth();
	    }

	    if(duration.getDay() >= 1 && duration.getDay() <10){
	      durationdate = "0" + duration.getDay();

	    }else{
	      durationdate  =""+duration.getDay();
	    }
	    if(duration.getHour() >= 1 && duration.getHour() <10){
	      durationhour = "0" + duration.getHour();
	    }else if(duration.getHour() == 12||duration.getHour() == 0){
		durationhour = "00";
	    }else{
	      durationhour =""+duration.getHour();
	    }

	    if(duration.getMinute() >= 1 && duration.getMinute() <10){
	     durationminute = "0" + duration.getMinute();
	   }else{
	     durationminute =""+duration.getMinute();
	    }

	   if(duration.getSecond() >= 1 && duration.getSecond() <10){
	     durationsecond = "0" + duration.getSecond();
	   }else{
	     durationsecond =""+duration.getSecond();
	    }
	    stringDuration = durationyear+"-"+durationmonth+"-"+durationdate+"-"+
	           durationhour+"-"+durationminute+"-"+durationsecond+" ";
	   
	  }
	  return stringDuration;
	      
	  }
	   /** to help in retrieval of Calendar value
	   *this method converts String into the Calendar value*/
	  public Calendar getCalendarOfString(String calendar){ 
	   Calendar newCalendar = Calendar.getInstance();
	   String calendaryear = calendar.substring(0,4);
	   String calendarmonth = calendar.substring(5,7);
	   String calendardate=calendar.substring(8,10);
	   String calendarhour=calendar.substring(11,13);;
	   String calendarminute =calendar.substring(14,16);
	   String calendarsecond=calendar.substring(17,19);
	    newCalendar.set(Integer.parseInt(calendaryear),
	                                     Integer.parseInt(calendarmonth),
	                        Integer.parseInt(calendardate), 
	                        Integer.parseInt(calendarhour),
	                        Integer.parseInt(calendarminute), 
	                        Integer.parseInt(calendarsecond));
	    return newCalendar;
	  }
	  /** to help in retrieval of GDuration value
	   *this method converts String into the GDuration value*/
	  public GDuration getDurationOfString(String duration){
	   String durationyear = duration.substring(0,4);
	   String durationmonth = duration.substring(5,7);
	   String durationdate=duration.substring(8,10);
	   String durationhour=duration.substring(11,13);;
	   String durationminute =duration.substring(14,16);
	   String durationsecond=duration.substring(17,19);
	   GDuration newDuration = new GDuration(+1,
	                 Integer.parseInt(durationyear),Integer.parseInt(durationmonth),
	                 Integer.parseInt(durationdate), Integer.parseInt(durationhour),
	                 Integer.parseInt(durationminute), 
	                 Integer.parseInt(durationsecond),null);
	   return newDuration;
	  }

	 
}

